﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class PreContractInfo: IPreContractInfo
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public PreContractInfo(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.PreContractInfo GetPreContractInfo(System.Int32 preContractID)
		{
			BEGDC.PreContract.DAL.PreContractInfo preContractInfo = BEGDC.PreContract.DAL.PreContractInfo.TryFind(preContractID);

            return preContractInfo;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetPreContractInfo_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM PreContractInfo";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeletePreContractInfo(System.Int32 preContractID)
		{
			try
            {
				BEGDC.PreContract.DAL.PreContractInfo preContractInfo = BEGDC.PreContract.DAL.PreContractInfo.TryFind(preContractID);
                preContractInfo.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfo", string.Format("Error occured when deleting the PreContractInfo, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddPreContractInfo(System.String preContractNo, BEGDC.PreContract.DAL.DicItem statusID, BEGDC.PreContract.DAL.DicItem typeID, BEGDC.PreContract.DAL.Product productID, Nullables.NullableDateTime promisedDate, Nullables.NullableDecimal cEWPromised, Nullables.NullableDecimal orionPrice, Nullables.NullableDecimal salePrice, Nullables.NullableDateTime signDue, Nullables.NullableDateTime signActual, System.String contractName, Nullables.NullableDateTime eRPReceived, System.String orionNo, System.Boolean delivered, System.String currentSituation, Nullables.NullableDateTime blackDate, Nullables.NullableDateTime tradeMonth, Nullables.NullableDateTime modifiedDate, System.String aCRNo, System.Boolean aSBorLucent, System.String applicant, System.String applicantEmail, System.String principal, System.String principalEmail, BEGDC.PreContract.DAL.Province provinceID, BEGDC.PreContract.DAL.CAT cATID, BEGDC.PreContract.DAL.BD bDID, System.String customerName, System.String customField01, System.String customField02, System.String customField03, System.String customField04, Nullables.NullableDecimal deliveryPercent, System.String advancedContract, System.Boolean isRisk)	
		{
			try
            {
				BEGDC.PreContract.DAL.PreContractInfo preContractInfo = new BEGDC.PreContract.DAL.PreContractInfo();
				
				preContractInfo.PreContractNo = preContractNo;
				preContractInfo.StatusID = statusID;
				preContractInfo.TypeID = typeID;
				preContractInfo.ProductID = productID;
				preContractInfo.PromisedDate = promisedDate;
				preContractInfo.CEWPromised = cEWPromised;
				preContractInfo.OrionPrice = orionPrice;
				preContractInfo.SalePrice = salePrice;
				preContractInfo.SignDue = signDue;
				preContractInfo.SignActual = signActual;
				preContractInfo.ContractName = contractName;
				preContractInfo.ERPReceived = eRPReceived;
				preContractInfo.OrionNo = orionNo;
				preContractInfo.Delivered = delivered;
				preContractInfo.CurrentSituation = currentSituation;
				preContractInfo.BlackDate = blackDate;
				preContractInfo.TradeMonth = tradeMonth;
				preContractInfo.ModifiedDate = modifiedDate;
				preContractInfo.ACRNo = aCRNo;
				preContractInfo.ASBorLucent = aSBorLucent;
				preContractInfo.Applicant = applicant;
				preContractInfo.ApplicantEmail = applicantEmail;
				preContractInfo.Principal = principal;
				preContractInfo.PrincipalEmail = principalEmail;
				preContractInfo.ProvinceID = provinceID;
				preContractInfo.CATID = cATID;
				preContractInfo.BDID = bDID;
				preContractInfo.CustomerName = customerName;
				preContractInfo.CustomField01 = customField01;
				preContractInfo.CustomField02 = customField02;
				preContractInfo.CustomField03 = customField03;
				preContractInfo.CustomField04 = customField04;
				preContractInfo.DeliveryPercent = deliveryPercent;
				preContractInfo.AdvancedContract = advancedContract;
				preContractInfo.IsRisk = isRisk;
				preContractInfo.CreateAndFlush();
				
				return preContractInfo.PreContractID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfo", string.Format("Error occured when adding a new PreContractInfo, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdatePreContractInfo(System.Int32 preContractID, System.String preContractNo, BEGDC.PreContract.DAL.DicItem statusID, BEGDC.PreContract.DAL.DicItem typeID, BEGDC.PreContract.DAL.Product productID, Nullables.NullableDateTime promisedDate, Nullables.NullableDecimal cEWPromised, Nullables.NullableDecimal orionPrice, Nullables.NullableDecimal salePrice, Nullables.NullableDateTime signDue, Nullables.NullableDateTime signActual, System.String contractName, Nullables.NullableDateTime eRPReceived, System.String orionNo, System.Boolean delivered, System.String currentSituation, Nullables.NullableDateTime blackDate, Nullables.NullableDateTime tradeMonth, Nullables.NullableDateTime modifiedDate, System.String aCRNo, System.Boolean aSBorLucent, System.String applicant, System.String applicantEmail, System.String principal, System.String principalEmail, BEGDC.PreContract.DAL.Province provinceID, BEGDC.PreContract.DAL.CAT cATID, BEGDC.PreContract.DAL.BD bDID, System.String customerName, System.String customField01, System.String customField02, System.String customField03, System.String customField04, Nullables.NullableDecimal deliveryPercent, System.String advancedContract, System.Boolean isRisk)
		{
			try
            {
                BEGDC.PreContract.DAL.PreContractInfo preContractInfo = GetPreContractInfo(preContractID);

				preContractInfo.PreContractNo = preContractNo;
				preContractInfo.StatusID = statusID;
				preContractInfo.TypeID = typeID;
				preContractInfo.ProductID = productID;
				preContractInfo.PromisedDate = promisedDate;
				preContractInfo.CEWPromised = cEWPromised;
				preContractInfo.OrionPrice = orionPrice;
				preContractInfo.SalePrice = salePrice;
				preContractInfo.SignDue = signDue;
				preContractInfo.SignActual = signActual;
				preContractInfo.ContractName = contractName;
				preContractInfo.ERPReceived = eRPReceived;
				preContractInfo.OrionNo = orionNo;
				preContractInfo.Delivered = delivered;
				preContractInfo.CurrentSituation = currentSituation;
				preContractInfo.BlackDate = blackDate;
				preContractInfo.TradeMonth = tradeMonth;
				preContractInfo.ModifiedDate = modifiedDate;
				preContractInfo.ACRNo = aCRNo;
				preContractInfo.ASBorLucent = aSBorLucent;
				preContractInfo.Applicant = applicant;
				preContractInfo.ApplicantEmail = applicantEmail;
				preContractInfo.Principal = principal;
				preContractInfo.PrincipalEmail = principalEmail;
				preContractInfo.ProvinceID = provinceID;
				preContractInfo.CATID = cATID;
				preContractInfo.BDID = bDID;
				preContractInfo.CustomerName = customerName;
				preContractInfo.CustomField01 = customField01;
				preContractInfo.CustomField02 = customField02;
				preContractInfo.CustomField03 = customField03;
				preContractInfo.CustomField04 = customField04;
				preContractInfo.DeliveryPercent = deliveryPercent;
				preContractInfo.AdvancedContract = advancedContract;
				preContractInfo.IsRisk = isRisk;
				preContractInfo.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfo", string.Format("Error occured when updating the PreContractInfo, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



